package com.ruihang.generalibrary.ui.util;

import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.view.View;

import com.jiongbull.jlog.JLog;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by rh008 on 2017/7/25.
 */

public class DefaultTextTimerCallback<T extends AppCompatTextView> implements TimerViewHelper.TimerViewCallback<T> {

    public static final long DURATION_UNLIMIT=-5L;

    private long time;

    private long timeStart;
    private long timeDuration=DURATION_UNLIMIT;
    private long timePassed;

    private long zeroTime;

    private Date timeDate;
    private SimpleDateFormat format;

    private String stopText;

    private boolean isRunning;

    public DefaultTextTimerCallback(String stopText) {
        this();
        setStopText(stopText);
//        JLog.e("stopText="+stopText+" this "+this.stopText);
    }

    public DefaultTextTimerCallback() {
        Calendar c=Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY,0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        zeroTime=c.getTimeInMillis();
    }

    @Override
    public void onTimerStart(T tv, TimerViewHelper<T> helper) {
//        JLog.e("timer start tv="+tv);
        if (tv!=null){
            time=timeStart+zeroTime;
            timePassed=timeStart;
            if (timeDate==null){
                timeDate=new Date(time);
            }else {
                timeDate.setTime(time);
            }
            if (format==null){
                format=new SimpleDateFormat("HH:mm:ss");
            }
            tv.setText(format.format(timeDate));
            isRunning=true;
        }
    }

    @Override
    public boolean onTimerRefresh(T tv, TimerViewHelper<T> helper,long timeInterval) {
//        JLog.e("timer refresh timeDuration="+timeDuration+"   time="+time+"  timePassed="+timePassed
//                +"  timeInterval="+timeInterval+"\n"+tv);
        if (tv!=null) {
            if (timePassed < timeDuration || timeDuration == DURATION_UNLIMIT) {
                time += timeInterval;
                timePassed+=timeInterval;
                timeDate.setTime(time);
                tv.setText(format.format(timeDate));
                return true;
            }
        }
        return false;
    }

    @Override
    public void onTimerStop(T tv, TimerViewHelper<T> helper) {
//        JLog.e("timer stop tv="+tv);
        isRunning=false;
        if (tv!=null && !TextUtils.isEmpty(stopText)){
            tv.setText(stopText);
        }
    }

    public long getTimeStart() {
        return timeStart;
    }

    public void setTimeStart(long timeStart) {
        if (timeStart>0) {
            this.timeStart = timeStart;
        }
    }

    public long getTimeDuration() {
        return timeDuration;
    }

    public void setTimeDuration(long timeDuration) {
        if (timeDuration>0) {
            this.timeDuration = timeDuration;
        }
    }

    public String getStopText() {
        return stopText;
    }

    public void setStopText(String stopText) {
        this.stopText = stopText;

    }
}
